import * as Cesium from 'cesium'
import gsap from 'gsap'
export default class RadarPropery{
    constructor(name = new Cesium.Color(0.7, 0.6, 1.0, 1.0)){
        this.name = name
        
        this.definitionChanged = new Cesium.Event()

        Cesium.Material._materialCache.addMaterial('Radar', {
            fabric: {
                type: 'Radar',
                uniforms: {
                    uTime: 0
                },
                source: `
                    czm_material czm_getMaterial(czm_materialInput materialInput)
                    {
                        czm_material material = czm_getDefaultMaterial(materialInput);
                        vec2 newSt = mat2(
                            cos(uTime),-sin(uTime),
                            sin(uTime),cos(uTime)
                        )*(materialInput.st-0.5);
                        newSt = newSt + 0.5;
                        vec2 st = newSt;
                        float alpha = 1.0 - step(0.5, distance(st, vec2(0.5)));
                        float angle = atan(st.x-0.5,st.y-0.5);
                        float strength = (angle+3.1416)/6.2832;
                        alpha = alpha * strength;
                        material.alpha = alpha;

                        material.diffuse = vec3(st.x, st.y,1.0);
                        return material;
                    }
                `
            }
        })
        this.params = {
            uTime: 0
        }
        gsap.to(this.params, {
            uTime: 6.28,
            duration: 1,
            repeat: -1,
            ease: 'linear'
        })

    }
    getType() {
        return 'Radar'
    }
    getValue(time, reuslt) {
        reuslt.uTime = this.params.uTime
        return reuslt
    }
    equals(other) {
        return (other instanceof RadarPropery && this.name === other.name)
    }
}